﻿#include "Graph3DSettings.h"
#include <QVBoxLayout>
#include <QObject>
#include <ElaText.h>
#include <ElaLineEdit.h>
#include <ElaSpinBox.h>
#include <ElaComboBox.h>
#include <ElaPivot.h>
#include <QRegExp>
#include <ElaTabWidget.h>
#include <QTabBar.h>

namespace pst
{
    Graph3DSettings::Graph3DSettings(QWidget* parent)
        : T_BasePage(parent)
        , m_page1CentralWidget{ nullptr }
        , m_page2CentralWidget{ nullptr }
        , m_tabWidget{ nullptr }
        , _page1Layout{ nullptr }
        , _page2Layout{ nullptr }
        , _titleFontSize{ 14 }
    {
        //设置基本布局属性
        //setFixedWidth(300);
        setContentsMargins(0, 0, 0, 0);
        addTopTabPivotUI();
    }

    Graph3DSettings::~Graph3DSettings()
    {
    }

    void Graph3DSettings::setScalarBarTitleName(const QString& titleName)
    {
        m_scalarBarTitleEditor->setText(titleName);
    }

    QString Graph3DSettings::getScalarBarTitleName()
    {
        return m_scalarBarTitleEditor->text();
    }

    void Graph3DSettings::setScalarBarMinMaxValue(double minValue, double maxValue)
    {
        m_minValueEditor->setText(QString::number(minValue));
        m_maxValueEditor->setText(QString::number(maxValue));
    }

    void Graph3DSettings::getScalarBarMinMaxValue(double& minValue, double& maxValue)
    {
        minValue = m_minValueEditor->text().toDouble();
        maxValue = m_maxValueEditor->text().toDouble();
    }

    void Graph3DSettings::addTopTabPivotUI()
    {
        //选项卡切换标题
        ElaPivot* _pivot = new ElaPivot(this);
        _pivot->setPivotSpacing(12);
        _pivot->setMarkWidth(40);
        _pivot->appendPivot("数据");
        _pivot->appendPivot("显示");
        _pivot->setCurrentIndex(0);
        _pivot->setTextPixelSize(16);
        setCustomWidget(_pivot);
        //---------- 第一页内容
        m_page1CentralWidget = new QWidget(this);
        m_page2CentralWidget = new QWidget(this);
        //选项卡视图
        m_tabWidget = new ElaTabWidget(this);
        m_tabWidget->tabBar()->hide();
        m_tabWidget->addTab(m_page1CentralWidget, "");
        m_tabWidget->addTab(m_page2CentralWidget, "");
        connect(_pivot, &ElaPivot::pivotClicked, this, [=](int index) {
            m_tabWidget->setCurrentIndex(index); });

        addCentralWidget(m_tabWidget, true, true, 0);

        _page1Layout = new QVBoxLayout(m_page1CentralWidget);
        _page2Layout = new QVBoxLayout(m_page2CentralWidget);

        addDataPageUI();
        addShowPageUI();
    }

    void Graph3DSettings::addDataPageUI()
    {
        //求解类型
        ElaText* solveTypeText = new ElaText("求解类型", this);
        solveTypeText->setTextPixelSize(_titleFontSize);
        ElaComboBox* solveTypeComboBox = new ElaComboBox(this);
        _page1Layout->addWidget(solveTypeText);
        _page1Layout->addWidget(solveTypeComboBox);

        // 固定值

        ElaText* fixedValueText = new ElaText("固定值", this);
        fixedValueText->setTextPixelSize(_titleFontSize);
        ElaComboBox* fixedValueComboBox = new ElaComboBox(this);
        _page1Layout->addWidget(fixedValueText);
        _page1Layout->addWidget(fixedValueComboBox);

        //类别
        ElaText* categoryText = new ElaText("类别", this);
        categoryText->setTextPixelSize(_titleFontSize);
        ElaComboBox* categoryTextComboBox = new ElaComboBox(this);
        _page1Layout->addWidget(categoryText);
        _page1Layout->addWidget(categoryTextComboBox);
        _page1Layout->addStretch();
    }

    void Graph3DSettings::addShowPageUI()
    {
        //横轴标题
        ElaText* scalarBarTitleText = new ElaText("横轴标题", this);
        scalarBarTitleText->setTextPixelSize(_titleFontSize);
        m_scalarBarTitleEditor = new ElaLineEdit(this);
        m_scalarBarTitleEditor->setText("标量场");
        connect(m_scalarBarTitleEditor, &ElaLineEdit::textChanged,
            this, [this]() { emit signal_scalarBarTitleChanged(m_scalarBarTitleEditor->text()); });

        QHBoxLayout* tieleNameLayout = new QHBoxLayout();
        tieleNameLayout->addWidget(scalarBarTitleText);
        tieleNameLayout->addWidget(m_scalarBarTitleEditor);
        _page2Layout->addLayout(tieleNameLayout);

        //字体字号
        ElaText* fontText = new ElaText("字体", this);
        fontText->setTextPixelSize(_titleFontSize);
        m_fontChosenComboBox = new ElaComboBox(this);
        QStringList _fontFamilyComboListX{ "宋体", "微软雅黑" };
        m_fontChosenComboBox->addItems(_fontFamilyComboListX);
        connect(m_fontChosenComboBox, QOverload<int>::of(&ElaComboBox::currentIndexChanged),
            this, [this]() { emit signal_scalarBarFontTypeChanged(m_fontChosenComboBox->currentText()); });

        //字号
        ElaText* m_fontSizeText = new ElaText("字号", this);
        m_fontSizeText->setTextPixelSize(_titleFontSize);
        m_fonSizeComboBox = new ElaComboBox(this);
        QStringList fontSize;
        for (int i = 1; i < 100; ++i)
        {
            fontSize << QString::number(i);
        }
        m_fonSizeComboBox->addItems(fontSize);
        connect(m_fonSizeComboBox, QOverload<int>::of(&ElaComboBox::currentIndexChanged),
            this, [this]() { emit signal_scalarBarFontSizeChanged(m_fonSizeComboBox->currentText().toInt()); });
        m_fonSizeComboBox->setCurrentText("24");

        QHBoxLayout* fontLayout = new QHBoxLayout();
        fontLayout->addWidget(fontText);
        fontLayout->addWidget(m_fontChosenComboBox);
        fontLayout->addWidget(m_fontSizeText);
        fontLayout->addWidget(m_fonSizeComboBox);
        _page2Layout->addLayout(fontLayout);

        //最大值
        ElaText* maxValueText = new ElaText("最大值", this);
        maxValueText->setTextPixelSize(_titleFontSize);
        m_maxValueEditor = new ElaLineEdit(this);
        QDoubleValidator* validator = new QDoubleValidator(-1000000000.0, 1000000000.0, 12, m_maxValueEditor);
        validator->setNotation(QDoubleValidator::StandardNotation);
        m_maxValueEditor->setValidator(validator);
        m_maxValueEditor->setText("10");
        connect(m_maxValueEditor, &ElaLineEdit::editingFinished,
            this, [this]() { emit signal_scalarBarMaxValueChanged(m_maxValueEditor->text().toDouble()); });
        QHBoxLayout* maxValueLayout = new QHBoxLayout();
        maxValueLayout->addWidget(maxValueText);
        maxValueLayout->addWidget(m_maxValueEditor);
        _page2Layout->addLayout(maxValueLayout);

        //最小值
        ElaText* minValueText = new ElaText("最小值", this);
        minValueText->setTextPixelSize(_titleFontSize);
        m_minValueEditor = new ElaLineEdit(this);
        QDoubleValidator* validatorMin = new QDoubleValidator(-1000000000.0, 1000000000.0, 12, m_minValueEditor);
        validatorMin->setNotation(QDoubleValidator::StandardNotation);
        m_minValueEditor->setValidator(validatorMin);
        m_minValueEditor->setText("1");
        connect(m_minValueEditor, &ElaLineEdit::editingFinished,
            this, [this]() { emit signal_scalarBarMinValueChanged(m_minValueEditor->text().toDouble()); });
        QHBoxLayout* minValueLayout = new QHBoxLayout();
        minValueLayout->addWidget(minValueText);
        minValueLayout->addWidget(m_minValueEditor);
        _page2Layout->addLayout(minValueLayout);


        _page2Layout->addStretch();

    }

}
